package DAO;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import Beans.Box;
import Beans.Package;
import Utils.HibernateUtil;
import Utils.TimeHelper;

public class BoxDao {

	private SessionFactory sessionFactory;

	public BoxDao() {
		sessionFactory = HibernateUtil.getSessionFactory();
	}

	Transaction tr = null;

	/**
	 * 返回该终端箱子的集合
	 * 
	 * @param terminalId
	 * @return
	 */
	public List<Box> boxStatusInThisTerminal(int terminalId) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		List<Box> boxes = null;
		Criteria c = session.createCriteria(Box.class);
		c.add(Restrictions.eq("terminalId", terminalId)); // select * from
															// boxstatus where
															// box_terminalId=terminalID;
		c.add(Restrictions.eq("isDelete", 0));
		c.addOrder(Order.asc("boxNum"));
		boxes = (ArrayList<Box>) c.list();
		tr.commit();
		session.close();
		return boxes;
	}

	/**
	 * 返回该终端有多少有多少占用的箱子
	 * 
	 * @param terminalId
	 * @return
	 */
	public int returnBoxesInThisTerminal(int terminalId) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		List<Box> boxes = null;
		Criteria c = session.createCriteria(Box.class);
		c.add(Restrictions.eq("terminalId", terminalId));
		c.add(Restrictions.eq("isDelete", 0));
		c.add(Restrictions.eq("boxStatus", 1));
		boxes = (ArrayList<Box>) c.list();
		tr.commit();
		session.close();
		return boxes.size();
	}

	/**
	 * 根据箱子类型和状态返回箱子数目
	 * 
	 * @param terminalId
	 * @param boxType
	 * @param boxStatus
	 * @return
	 */
	public int boxStatusDetail(int terminalId, int boxType, int boxStatus) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		List<Box> boxes = null;
		Criteria c = session.createCriteria(Box.class);
		c.add(Restrictions.eq("terminalId", terminalId));
		if (boxType != -1) {
			c.add(Restrictions.eq("boxType", boxType));
		}
		if (boxStatus != -1) {
			c.add(Restrictions.eq("boxStatus", boxStatus));
		}
		boxes = (ArrayList<Box>) c.list();
		tr.commit();
		session.close();
		return boxes.size();
	}

	/**
	 * 确定该终端是否有该箱子
	 * 
	 * @param terminalId
	 *            终端号
	 * @param boxNum
	 *            柜号
	 * @return 未取包裹信息
	 */
	public List<Box> getBoxByBoxNum(int terminalId, int boxNum) {

		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		List<Box> boxs = null;
		Criteria c = session.createCriteria(Box.class);
		c.add(Restrictions.eq("terminalId", terminalId));
		c.add(Restrictions.eq("boxNum", boxNum));
		boxs = (ArrayList<Box>) c.list();
		tr.commit();
		session.close();
		return boxs;
	}

	/**
	 * 根据箱子id返回箱子
	 * 
	 * @param terminalId
	 *            终端号
	 * @param boxNum
	 *            柜号
	 * @return 未取包裹信息
	 */
	public Box getBoxById(int boxId) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Box box = (Box) session.get(Box.class, boxId);
		tr.commit();
		session.close();
		return box;
	}

	/**
	 * 添加箱子信息
	 * 
	 * @param terminalID
	 * @param boxNum
	 * @param boxType
	 * @return
	 */
	public boolean addBox(int terminalID, int boxNum, int boxType) {

		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Box box = new Box();
		box.setBoxNum(boxNum);
		box.setBoxStatus(0);
		box.setBoxType(boxType);
		box.setTerminalId(terminalID);
		session.save(box);
		tr.commit();
		session.close();
		return true;

	}

	/**
	 * 修改箱子信息
	 * 
	 * @param terminalID
	 * @param boxNum
	 * @param boxType
	 * @return
	 */
	public boolean boxUpdate(Box box, int terminalID, int boxNum, int boxType) {

		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		box.setBoxNum(boxNum);
		// box.setBoxStatus(0);
		box.setIsDelete(0);
		box.setBoxType(boxType);
		box.setTerminalId(terminalID);
		session.update(box);
		tr.commit();
		session.close();
		return true;

	}

	/**
	 * 修改箱子信息
	 * 
	 * @param terminalID
	 * @param boxNum
	 * @param boxType
	 * @return
	 */
	public boolean updateBox(Box box) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.update(box);
		tr.commit();
		session.close();
		return true;
	}

	/**
	 * 删除柜子
	 */
	public void delete(Box box) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		box.setIsDelete(1);
		session.update(box);
		tr.commit();
		session.close();
	}

	/**
	 * 设置指定箱号可用 set box to be available
	 * 
	 * @param terminalId
	 * @param boxNum
	 * @return
	 */

	public boolean setBoxAvailable(int terminalId, int boxNum, int type) {
		return changeBoxStatus(terminalId, boxNum, 0, type, 0, 0);
	}

	/**
	 * 设置指定箱号故障
	 * 
	 * @param terminalId
	 * @param boxNum
	 * @return
	 */
	public boolean setBoxFault(int terminalId, int boxNum) {
		return changeBoxStatus(terminalId, boxNum, 5, -1, 0, 0);
	}

	/**
	 * 设置指定箱号占用
	 * 
	 * @param terminalId
	 * @param boxNum
	 * @return
	 */
	public boolean setBoxOccupation(int terminalId, int boxNum, int goodsID,
			int parcelID) {
		return changeBoxStatus(terminalId, boxNum, 1, -1, goodsID, parcelID);
	}

	/**
	 * 
	 * @param terminalId
	 * @param boxNum
	 * @param status
	 *            0.箱子可用 1.箱子不可用 5.箱子故障
	 * @param type
	 *            0,清空；1，开锁
	 * @return
	 */
	public boolean changeBoxStatus(int terminalId, int boxNum, int status,
			int type, int goodsID, int parcelID) {

		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		List<Box> boxes = null;
		Box box = null;

		Criteria c = session.createCriteria(Box.class);
		c.add(Restrictions.eq("terminalId", terminalId));
		c.add(Restrictions.eq("boxNum", boxNum));

		boxes = (ArrayList<Box>) c.list();

		if (boxes.iterator().hasNext()) {
			box = boxes.iterator().next();
			box.setBoxStatus(status);
			box.setGoodsID(goodsID);
			box.setParcelID(parcelID);
			if (status == 0) {
				if (type == 1) {
					box.setForceOpen(1);
				} else {
					box.setForceOpen(2);
				}
				box.setEffectTime(new Timestamp(TimeHelper.getDeadTime(
						new Date(), 5)));
			} else if (status == 1) {
				if (goodsID == 0) {
					box.setForceOpen(0);
					box.setEffectTime(null);
				} else {
					box.setForceOpen(0);
					box.setEffectTime(null);
//					if (type == 1) {
//						box.setForceOpen(1);
//					} else {
//						box.setForceOpen(2);
//					}
//					box.setEffectTime(new Timestamp(TimeHelper.getDeadTime(
//							new Date(), 5)));
				}
			} else {
				// box.setForceOpen(0);
			}
			session.update(box);
			tr.commit();
			session.close();
			return true;
		} else {
			session.close();
			return false;
		}

	}

	/**
	 * 查看指定终端指定柜号中的未取包裹信息
	 * 
	 * @param terminalId
	 *            终端号
	 * @param boxNum
	 *            柜号
	 * @return 未取包裹信息
	 */
	public List<Package> getPacakgeDetailsByBoxNum(int terminalId, int boxNum) {

		TerminalDao terminalDao = new TerminalDao();
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		List<Package> packages = null;
		Criteria c = session.createCriteria(Package.class);
		c.add(Restrictions.eq("boxId", boxNum));
		c.add(Restrictions.eq("terminal",
				terminalDao.getTerminalById(terminalId)));
		c.addOrder(Order.desc("storeTime"));
		packages = (ArrayList<Package>) c.list();
		tr.commit();
		session.close();
		return packages;
	}

	public Box boxStatusByBoxID(int boxID) {
		return null;
	}

	/**
	 * 查询某终端下所有箱格
	 * 
	 * @param terminalId
	 * @return
	 */
	public List<Box> getAllUseBoxOfTerminal(int terminalId) {
		Session session = sessionFactory.openSession();
		String hql = "from Box b where b.terminalId=? and b.boxStatus=1";
		Query query = session.createQuery(hql);
		query.setParameter(0, terminalId);
		List<Box> list = query.list();
		session.close();
		return list;
	}

	public static void main(String[] args) {
		System.out.println(new BoxDao().getAllUseBoxOfTerminal(3).size());
	}
}
